package com.xw.controller.xw;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xw.controller.base.BaseController;
import com.xw.entity.system.User;
import com.xw.service.xw.cycf.CycfService;
import com.xw.util.AppUtil;
import com.xw.util.Const;
import com.xw.util.PageData;
/**
 * 类名称: CycfController
 * 类描述: 常用处方controller
 * @author : gce
 * @date   : 2017-12-1
 */
@Controller
@RequestMapping( value = "/cycf")
public class CycfController extends BaseController {
	
	@Resource(name="cycfService")
	private CycfService cycfService;
	
	/**
	 * 查询常用处方名称是否存在
	 * @return: ModelAndView
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2018-1-5
	 */
	@RequestMapping( value = "/hasByCycfName")
	@ResponseBody
	public Object hasByCycfName() throws Exception{
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		pd = this.getPageData();
		String cycfName = pd.getString("cycfName").trim();
		boolean flag = cycfService.hasByCycfName(cycfName);
		map.put("result", flag?"yes":"no");
		return AppUtil.returnObject(pd, map);
	}
	
	/**
	 * 添加常用处方
	 * @return: ModelAndView
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2018-1-5
	 */
	@RequestMapping( value = "/add")
	@ResponseBody
	public Object addCycf() throws Exception{
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		pd = this.getPageData();
		
		//获取所有药品ids、常用处方名称
		String ypIds = pd.getString("ypIds");
		String cycfName =  pd.getString("cycfName").trim();
		String numbers = pd.getString("numbers");
		
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = new User();
		user = (User)session.getAttribute(Const.SESSION_USER);
		
		String[] ypIdArray = null;
		String[] numberArray = null;
		String msg = "success";
		
		if (null != ypIds && !"".equals(ypIds)) {
			ypIdArray = ypIds.split(",");
		} else {
			msg = "添加失败1";
		}
		if (null != numbers && !"".equals(numbers)) {
			numberArray = numbers.split(",");
		} else {
			msg = "添加失败2";
		}
		PageData pd2 = new PageData();
		pd2.put("USER_ID", user.getUSER_ID());
		pd2.put("cycfName", cycfName);
		int result = cycfService.add(pd2, ypIdArray, numberArray);
		if(result>0){
			 msg = "success";
		}else{
			 msg = "添加失败";
		}
		map.put("msg", msg);
		return AppUtil.returnObject(pd, map);
	}
	
	/**
	 * 查询常用处方 ，根据医师id
	 * @param page
	 * @throws Exception
	 * @return: ModelAndView
	 * @author: gce
	 * @date  : 2017-12-5
	 */
	@RequestMapping( value = "/cycfList")
	@ResponseBody
	public Object cycfList() throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		User user = new User();
		user = (User)session.getAttribute(Const.SESSION_USER);
		PageData pd = new PageData();
		pd.put("USER_ID", user.getUSER_ID());
		List<PageData> cycfList = cycfService.list(pd);
		if(cycfList.size()>0){
			map.put("msg", "success");
			map.put("cycfList", cycfList);
		}else{
			map.put("msg", "暂无常用处方");
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 查询常用处方药品，根据常用处方ID
	 * @param page
	 * @throws Exception
	 * @return: ModelAndView
	 * @author: gce
	 * @date  : 2017-12-5
	 */
	@RequestMapping( value = "/cycfypList")
	@ResponseBody
	public Object cycfypList() throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData> cycfypList = cycfService.cycfyplist(pd);
		map.put("cycfypList", cycfypList);
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 删除常用处方
	 * @return: ModelAndView
	 * @author: gce
	 * @throws Exception 
	 * @date  : 2018-1-5
	 */
	@RequestMapping( value = "/delCycf")
	@ResponseBody
	public Object delCycf() throws Exception{
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		pd = this.getPageData();
		//获取常用处方id
		String msg = "success";
		int result = cycfService.delCycf(pd);		
		if(result>0){
			 msg = "success";
		}else{
			 msg = "删除失败";
		}
		map.put("msg", msg);
		return AppUtil.returnObject(pd, map);
	}
	
}
